﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Litografia.BusinessComponents;
using Litografia.BusinessEntities;

namespace Litografia.UI.Reports
{
    public partial class HorasPorOperarioForm : BaseForm
    {
        public HorasPorOperarioForm()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private void HorasPorOperarioForm_Load(object sender, EventArgs e)
        {
            ConfigureToolBar(toolStrip, ToolBarButtons.Print | ToolBarButtons.Close);
            LoadData();
            toolStrip.ItemClicked += toolStrip_ItemClicked;
        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch ((ToolBarButtons)e.ClickedItem.Tag)
            {
                case ToolBarButtons.Print:
                    if (ValidateEntries())
                        Print();
                    break;
                case ToolBarButtons.Close:
                    Close();
                    break;
            }
        }
        
        private void datesInSearchCheck_CheckedChanged(object sender, EventArgs e)
        {
            ToggleDates();
        }

        private void todosCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            var isChecked = todosCheckBox.Checked;
            for(var i=0; i < operarioListBox.Items.Count; i++)
                operarioListBox.SetItemChecked(i, isChecked);

            todosCheckBox.Text = isChecked ? "Desmarcar todos" : "Marcar todos";
        }

        #endregion

        #region Methods: Non-Public

        private void LoadData()
        {   
            datesInSearchCheck.Checked = false;
            dateBegin.Value = DateTime.Now;
            dateEnd.Value = DateTime.Now;

            var source = BCEntity.GetList(EntityType.Operario, "");
            operarioListBox.DataSource = source;
            operarioListBox.DisplayMember = "Nombre";
            operarioListBox.ValueMember = "Id";
        }

        public void ToggleDates()
        {
            dateBegin.Enabled = datesInSearchCheck.Checked;
            dateEnd.Enabled = datesInSearchCheck.Checked;
        }

        private void Print()
        {
            var operarioList = new List<int>();
            foreach (BEEntity item in operarioListBox.CheckedItems)
                operarioList.Add(Convert.ToInt32(item.Id));

            DateTime? beginDate = null;
            DateTime? endDate = null;

            if (datesInSearchCheck.Checked)
            {
                beginDate = dateBegin.Value;
                endDate = dateEnd.Value;
            }

            BEHorasLaboradasReporte data = BCOT.GetHorasLaboradas(operarioList, beginDate, endDate);
            var report = new ReportViewerForm();
            report.BuildReport(data, "Litografia.UI.Reports.HorasLaboradas.rdlc");
            ShowScreen(report);
            Close();
        }

        private bool ValidateEntries()
        {
            if (operarioListBox.CheckedItems.Count == 0)
            {
                ShowError("Debe seleccionar al menos un operario");
                return false;
            }
            return true;
        }

        #endregion

    }
}
